<div class="m-bg">
<h1>SQL Server DATEPART() 函数</h1>
<h2>定义和用法</h2> <p>DATEPART() 函数用于返回日期/时间的单独部分，比如年、月、日、小时、分钟等等。</p> <h3><b>语法</b></h3> <div class="code notranslate"><pre><code class="language-sql"><div> DATEPART(datepart,date)</div></code></pre></div> <p>date 参数是合法的日期表达式。datepart 参数可以是下列的值：</p> <table class="reference notranslate">
<tr>
<th align="left">datepart</th> <th align="left">缩写</th> </tr>
<tr>
<td>年</td> <td>yy, yyyy</td> </tr>
<tr>
<td>季度</td> <td>qq, q</td> </tr>
<tr>
<td>月</td> <td>mm, m</td> </tr>
<tr>
<td>年中的日</td> <td>dy, y</td> </tr>
<tr>
<td>日</td> <td>dd, d</td> </tr>
<tr>
<td>周</td> <td>wk, ww</td> </tr>
<tr>
<td>星期</td> <td>dw, w</td> </tr>
<tr>
<td>小时</td> <td>hh</td> </tr>
<tr>
<td>分钟</td> <td>mi, n</td> </tr>
<tr>
<td>秒</td> <td>ss, s</td> </tr>
<tr>
<td>毫秒</td> <td>ms</td> </tr>
<tr>
<td>微妙</td> <td>mcs</td> </tr>
<tr>
<td>纳秒</td> <td>ns</td> </tr>
</table>
<h3><b>实例</b></h3> <p>假设我们有如下的 "Orders" 表：</p> <table class="reference notranslate">
<tr>
<th align="left">OrderId</th> <th align="left">ProductName</th> <th align="left">OrderDate</th> </tr>
<tr>
<td>1</td> <td>Jarlsberg Cheese</td> <td>2008-11-11 13:23:44.657</td> </tr>
</table>
<p>下面是 SELECT 语句：</p> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT DATEPART(yyyy,OrderDate) AS OrderYear,<br/> DATEPART(mm,OrderDate) AS OrderMonth,<br/> DATEPART(dd,OrderDate) AS OrderDay,<br/> FROM Orders<br/> WHERE OrderId=1 </div></code></pre></div> <p>结果如下所示：</p> <table class="reference notranslate">
<tr>
<th align="left">OrderYear</th> <th align="left">OrderMonth</th> <th align="left">OrderDay</th> </tr>
<tr>
<td>2008</td> <td>11</td> <td>11</td> </tr>
</table>
<div class="text-center padding-10 margin-t-5">
</div>
</div>